from flask import Flask, request, render_template, jsonify
import os
import datetime


app = Flask(__name__)

# 用于存储已提交IP地址的文件名
IP_FILE = 'submitted_ips.txt'
# 用于存储提交数据的文件名
DATA_FILE = 'submissions.txt'

@app.route('/', methods=['GET'])
def index():
    return render_template('index.html')

@app.route('/submit', methods=['POST'])
def submit():
    name = request.form.get('name')
    ip = request.remote_addr
    current_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

    # 检查IP是否已经提交过
    if ip_already_submitted(ip):
        return jsonify({'message': 'Your IP has already submitted.'}), 400

    # 保存提交的数据到文件
    with open(DATA_FILE, 'a') as f:
        f.write(f'Name: {name}, IP: {ip}, Time: {current_time}\n')

    # 保存IP到已提交IP文件
    with open(IP_FILE, 'a') as f:
        f.write(ip + '\n')

    return jsonify({'message': 'Submission successful.'}), 200

def ip_already_submitted(ip):
    if not os.path.isfile(IP_FILE):
        return False
    with open(IP_FILE, 'r') as f:


      return render_template(index.html)


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)